package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.bean.ActivityBean;
import cst.neu.yuejian_31.bean.ActivityCollectBean;
import cst.neu.yuejian_31.bean.ActivityStateBean;
import cst.neu.yuejian_31.iface.ActivityCollectListener;
import cst.neu.yuejian_31.iface.ActivityCollectStateListener;
import cst.neu.yuejian_31.iface.ActivityDetailListener;
import cst.neu.yuejian_31.model.ActivityCollectMdoel;
import cst.neu.yuejian_31.model.ActivityCollectStateModel;
import cst.neu.yuejian_31.model.ActivityModel;


public class ActivityDetailActivity extends BaseActivity implements View.OnClickListener,ActivityCollectListener,ActivityCollectStateListener,ActivityDetailListener{
private Context context=null;


    private TextView activityName,activityIntro,activityAddress,activityTel,activityTime;
    private ImageView activityPic;
    private String userId,activityId,activityaddress,activitymoney,activityname,activitypic;
    private Button activityBtn;
    private int state;
    private int collectState;
    private String s;
    private ImageView favorImageButton,btn_return;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();

        ActivityCollectStateModel model=new ActivityCollectStateModel();
        model.getactivityState(userId,activityId,this);
        favorImageButton.setOnClickListener(this);
        btn_return.setOnClickListener(this);
        getActivityId();
        initdata();

    }

    private void initView() {
        Intent intent=getIntent();
        activityId=intent.getStringExtra("activity_id");
        userId=getUserId();
        activityName= (TextView) findViewById(R.id.match_name_detail);
        activityIntro= (TextView) findViewById(R.id.match_intro);
        activityAddress=(TextView) findViewById(R.id.match_adress);
        activityPic=(ImageView) findViewById(R.id.imageView_match_detail);
        activityTel=(TextView)findViewById(R.id.match_tel);
        activityTime=(TextView)findViewById(R.id.match_time);
        favorImageButton=(ImageView)findViewById(R.id.imageView_activity_collect);
        activityBtn=(Button)findViewById(R.id.button_activity_join);
        btn_return= (ImageView) findViewById(R.id.imagview_return_acytivitydetail);
        activityBtn.setOnClickListener(this);
    }

    private void getActivityId() {
            Intent intent=getIntent();
        activityId=intent.getStringExtra("activity_id");
        Toast.makeText(ActivityDetailActivity.this,activityId, Toast.LENGTH_SHORT).show();

//        Toast.makeText(CourseDetailActivity.this,classId+userId, Toast.LENGTH_SHORT).show();



    }

    private void initdata() {
        ActivityModel model=new ActivityModel();
       model.getactivitydetail(activityId,this);
        Intent intent=getIntent();
        activityId=intent.getStringExtra("activity_id");
        activityaddress=intent.getStringExtra("activity_address");
        activitymoney=intent.getStringExtra("activity_money");
        activityname=intent.getStringExtra("activity_name");
        activitypic=intent.getStringExtra("activity_pic");
//        Picasso.with(context).load("http://10.0.2.2/neu16_yuejian/Public/uploads/activity/"+intent.getStringExtra("activity_pic").toString()).into(activityPic);
    }

    @Override
    public void onSuccess(ActivityStateBean activityStateBean) {
        state=activityStateBean.getState();
        s=String.valueOf(state);
        Toast.makeText(ActivityDetailActivity.this,s, Toast.LENGTH_SHORT).show();
        if(state==1){
            favorImageButton.setImageResource(R.drawable.ic_collect);

        }else {
            favorImageButton.setImageResource(R.drawable.ic_uncollect);
        }
    }

    @Override
    public void onSuccess(ActivityCollectBean activityCollectBean) {
        collectState=activityCollectBean.getSuccess();
        if(collectState==1){
            favorImageButton.setImageResource(R.drawable.ic_collect);
            Toast.makeText(ActivityDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();

        }else {
            favorImageButton.setImageResource(R.drawable.ic_uncollect);
            Toast.makeText(ActivityDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_activity_collect:
                doFavor();
                break;
            case R.id.button_activity_join:
                Intent intent=new Intent(ActivityDetailActivity.this,ActivityOrderActivity.class);
                intent.putExtra("activity_id",activityId);
                intent.putExtra("activity_name",activityname);
                intent.putExtra("activity_money",activitymoney);
                intent.putExtra("activity_address",activityaddress);
                intent.putExtra("activity_pic",activitypic);
                startActivity(intent);
                break;
            case R.id.imagview_return_acytivitydetail:
                finish();
                break;
        }
    }

    private void doFavor() {
       ActivityCollectMdoel favorModel=new ActivityCollectMdoel();
        favorModel.activityCollect(userId,activityId,this);
    }
    @Override
    public void onResponse(ActivityBean activityBean) {
        activityName.setText(activityBean.getActivity_name());
        activityIntro.setText(activityBean.getActivity_intro());
        activityTel.setText(activityBean.getActivity_tel());
        activityAddress.setText(activityBean.getActivity_address());
        activityTime.setText(activityBean.getActivity_time());
       Picasso.with(ActivityDetailActivity.this).load(Constant.ACTIVITY_URL+activityBean.getActivity_pic()).into(activityPic);


    }

    @Override
    public void onFail(String msg) {

    }
}
